package com.zxn.rv;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.zxn.rv.utils.Constant;
import com.zxn.rv.utils.FileUtils;
import com.zxn.rv.utils.ViewCenterUtils;

import java.io.File;
import java.util.List;

import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;

/**
 * @author: njb
 * @date: 2020/2/27 0027 17:30
 * @desc: 保存文件
 */
public class SaveFileActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks {
    private static final int REQUEST_CODE_QRCODE_PERMISSIONS = 1001;
    private TextView tvSave, tvRead;
    private EditText editText;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_save_file);
        askPermission();
        initView();

    }

    @AfterPermissionGranted(REQUEST_CODE_QRCODE_PERMISSIONS)
    private void askPermission() {
        String[] perms = {Manifest.permission.WRITE_SETTINGS, Manifest.permission.READ_EXTERNAL_STORAGE};
        if (!EasyPermissions.hasPermissions(this, perms)) {
            EasyPermissions.requestPermissions(this, "保存文件需要读写权限", REQUEST_CODE_QRCODE_PERMISSIONS, perms);
        }
    }

    private void initView() {
        editText = findViewById(R.id.et_order);
        tvSave = findViewById(R.id.tv_save);
        tvRead = findViewById(R.id.tv_read);

        tvSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = editText.getText().toString();
                FileUtils.stringTxt(str);
                Toast.makeText(SaveFileActivity.this, "文件保存成功", Toast.LENGTH_SHORT).show();
            }
        });
        tvRead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                File file = new File(Constant.filePath, "/entrepreneurial.txt");
                editText.setText(FileUtils.getFileContent(file));
            }
        });
    }

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {

    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {

    }

}
